<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" size="lg" title="创建通知" scrollable no-close-on-backdrop>
        <div class="d-block text-center">
            <!--begin::div-->
            <div class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <label
                            class="col-lg-2 col-form-label">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题</label>
                        <div class="col-lg-4">
                            <input class="form-control" type="text" maxlength="100" v-model="notifyForm.title"
                                placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label">内&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;容</label>
                        <div class="col-lg-4">
                            <textarea class="form-control" rows="4" maxlength="800"  placeholder="请输入" v-model="notifyForm.content"></textarea>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label">接&nbsp;&nbsp;收&nbsp;&nbsp;人</label>
                        <div class="col-lg-4">
                            <div class="input-group">
                                <input type="text" readonly class="form-control m-input" v-model="notifyForm.createText" placeholder="请选择" aria-describedby="basic-addon2">
                                <div class="input-group-append">
                                    <span style="cursor:pointer;" class="input-group-text" id="basic-addon2" @click="resetUser();">
                                        <i class="fa fa-times"></i>
                                    </span>
                                </div>
                                <div class="input-group-append">
                                    <span  style='cursor:pointer;' class="input-group-text" @click='UserSelect()'>
                                        <i class="flaticon-user-ok"></i>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button>
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="addXtNotify">
                <i class="icon-xl fas fa-save"></i> 保存
            </button>
        </template>
        <NotifyUserSelect ref="notifyUserSelectRef" @change="doSetUser"></NotifyUserSelect><!--采用父窗口关闭传递的方法-->
    </b-modal>
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js";
import NotifyUserSelect from "@/view/sys/xt-notify/xt-notify-user-select.vue";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating } from "@/core/util/jehcUtil.js";

export default {
    data() {
        return {
            notifyForm: {
                title: "",
                content: "",
                createId:"",
                createText:"",
            }
        }
    },
    components: {
        NotifyUserSelect,
    },
    watch: {
    },
    mounted() {
       
    },
    
    methods: {
        resetUser(){
            this.notifyForm.createId = "";
            this.notifyForm.createText = "";
        },
        UserSelect(){
            this.$refs.notifyUserSelectRef.showModal();
        },
        doSetUser(data){
            if(data){
                let userId = data.map((item) => item.id);
                let names =  data.map((item) => item.name);
                this.notifyForm.createId = userId.join(",");
                this.notifyForm.createText = names.join(",");
            }
        },
        addXtNotify() {
            if (this.notifyForm.title === "") {
                handleAlert("请输入标题", "warning", 3)
                return;
            }
            if (this.notifyForm.content === "") {
                handleAlert("请输入内容", "warning", 3)
                return;
            }
            if (this.notifyForm.createId === "") {
                handleAlert("请选择接收人", "warning", 3)
                return;
            }
            // 保存前提示
            this.$confirm("确定保存?", "提示", { type: "warning", }).then(() => {
                apiUtil.post(process.env.VUE_APP_SYS_API + "/xtNotify/add", this.notifyForm).then(({ data }) => {
                    if (data.success) {
                        handleAlert("创建成功", "success", 3);
                        this.hideModal();//关闭窗体
                        this.$emit("change", data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                    } else {
                        handleAlert("创建失败", "error", 3);
                    }
                });
            }).catch(() => { });//注意这里，这里是重点！！！;   
        },
        showModal() {
            this.restForm();
            this.$refs['my-modal'].show();
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        restForm() {
            //重置
            Object.assign(this.$data.notifyForm, this.$options.data().notifyForm);
        },
    }
}
</script>